/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.component.comboBox;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.plaf.basic.BasicComboPopup;

import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JComboBoxPOJO;
/**
 * 下拉内容可设置高度的JComboBox  -----  list
 */
public class JComboBoxPopupHeight extends BasicComboPopup implements MouseListener {
	private static final long serialVersionUID = 1L;
	
	private JList list;
	private JComboBox box;
	private DefaultListModel listModel;
	
	public JComboBoxPopupHeight(JComboBox box) {
		super(box);
		this.box = box;
		init();
	}

	private void init() {
		
		list = new JList();
		
		listModel = new DefaultListModel();
		
		list.setFixedCellHeight(50); 
		list.setModel(listModel);
		list.setAutoscrolls(true);
		list.addMouseListener(this);
		list.setFont(Util.FONT_20_PLAIN);
		
		JScrollPane jsp = new JScrollPane(list); 
		jsp.setPreferredSize(new Dimension(200, 300));
		this.setLayout(new BorderLayout());
		this.add(jsp);
	}

	@Override
	protected void firePropertyChange(String propertyName, Object oldValue,Object newValue) {
		if (propertyName.equals("visible")) {
			if (oldValue.equals(Boolean.FALSE)&& newValue.equals(Boolean.TRUE)) { // SHOW
				try {
					if (box != null) {
						listModel.removeAllElements();
						DefaultComboBoxModel comboBoxModel=(DefaultComboBoxModel)box.getModel();
						for (int i = 0; i < comboBoxModel.getSize(); ++i) {
							JComboBoxPOJO po = (JComboBoxPOJO)comboBoxModel.getElementAt(i);
							listModel.addElement(po);
						}
					}
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			} else if (oldValue.equals(Boolean.TRUE)&& newValue.equals(Boolean.FALSE)) { // HIDE
			}
		}
		super.firePropertyChange(propertyName, oldValue, newValue);
	}

	@Override 
	public void mouseClicked(MouseEvent e) {
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		box.setSelectedItem(list.getSelectedValue());
		this.hide();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		
	}

	
//	public void hide(){
//		
//	}
}